//
//  gamedatamanager.h
//  battle-empire
//
//  Created by droidhang on 13-8-22.
//
//

#pragma once

#include "type.h"
#include "equipment.h"
#include "formation.h"
#include "campaigninfo.h"
#include "cardpack.h"

#define  GDM   GameDataManager::getInstance()

static const int FORMATION_COUNT = 4;

class GameDataManager {
public:
	static GameDataManager* getInstance();
	~GameDataManager();

	void init();

	string getEquipmentName(EquipmentType type, int index);
	string getEquipmentIconFileName(EquipmentType type, int index);
	string getEquipmentIconFileName(EquipmentType type, const string& name);

	void saveFormation(int index, const FormationInfo& formationInfo);

	FormationInfo *loadCampaignFormation(int episodeIndex, int chapterIndex);

	vector<string> getEquipmentsNames(EquipmentType type, EquipmentRarity rarity);

	DamageType getWeaponDamageType(BulletProjectileType type);

	void saveMoney();

private:
	GameDataManager();
	static GameDataManager* _instance;

	void loadUserInfo();

	void loadEquipmentsAttribute();
	void loadFormationsFromFiles();
	void loadFormationsFromDB();
	void loadCampaignsInfo();
	void loadCardPackInfo();
	void loadEquipmentsQuantity();

	void parseXmlNode(tinyxml2::XMLElement* xe, const char* tag, int& v);
	void parseXmlNode(tinyxml2::XMLElement* xe, const char* tag, float& v);
	void parseXmlNode(tinyxml2::XMLElement* xe, const char* tag, string& v);

	SquadOrder parseSquadOrder(char const *s);
	EquipmentActionType parseEquipmentActionType(const char *s);
	EquipmentActionTriggerType parseEquipmentActionTriggerType(const char *s);
	EquipmentActionTargetType parseEquipmentActionTargetType(const char *s);
	EquipmentActionTimeMode parseEquipmentActionTimeMode(const char *s);
	EquipmentActionModifyProperty parseEquipmentActionModifyProperty(const char *s);
	EquipmentActionSelectionType parseEquipmentActionSelectionType(const char *s);

public:
	map<string, EquipmentAttribute> equipmentsAttribute;
	map<string, int> equipmentsQuantity;
	map<int, string> equipmentIDMapping;

	EquipmentAttribute equipmentBlankAttribute;

	vector<string> creatures;
	vector<string> weapons;
	vector<string> armors;
	vector<string> shields;
	vector<string> specials;

	FormationInfo * formations[FORMATION_COUNT];

	vector<CampaignInfo> campaigns;
	vector<CardPackInfo> packs;
	vector<string> friends;

	int money;
	string playerName;
	string deviceID;

	char buf[300];
};
